﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Hr.DAL
{
    /// <summary>
    /// 数据访问类Hr_VacationRecord。
    /// </summary>
    public class Hr_VacationRecord
    {
        public Hr_VacationRecord()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Hr.Entity.Hr_VacationRecord model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Hr_VacationRecord(");
            strSql.Append("RecordId,UserId,UserName,DepartId,DepartName,DateTime,StartTime,EndTime,Days,VacationType,Reason)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@UserId,@UserName,@DepartId,@DepartName,@DateTime,@StartTime,@EndTime,@Days,@VacationType,@Reason)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, model.UserId);
            db.AddInParameter(dbCommand, "UserName", DbType.String, model.UserName);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "DepartName", DbType.String, model.DepartName);
            db.AddInParameter(dbCommand, "DateTime", DbType.DateTime, model.DateTime);
            db.AddInParameter(dbCommand, "StartTime", DbType.DateTime, model.StartTime);
            db.AddInParameter(dbCommand, "EndTime", DbType.DateTime, model.EndTime);
            db.AddInParameter(dbCommand, "Days", DbType.Decimal, model.Days);
            db.AddInParameter(dbCommand, "VacationType", DbType.AnsiString, model.VacationType);
            db.AddInParameter(dbCommand, "Reason", DbType.String, model.Reason);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Hr.Entity.Hr_VacationRecord model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Hr_VacationRecord set ");
            strSql.Append("UserId=@UserId,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("DepartId=@DepartId,");
            strSql.Append("DepartName=@DepartName,");
            strSql.Append("DateTime=@DateTime,");
            strSql.Append("StartTime=@StartTime,");
            strSql.Append("EndTime=@EndTime,");
            strSql.Append("Days=@Days,");
            strSql.Append("VacationType=@VacationType,");
            strSql.Append("Reason=@Reason");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "UserId", DbType.Guid, model.UserId);
            db.AddInParameter(dbCommand, "UserName", DbType.String, model.UserName);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "DepartName", DbType.String, model.DepartName);
            db.AddInParameter(dbCommand, "DateTime", DbType.DateTime, model.DateTime);
            db.AddInParameter(dbCommand, "StartTime", DbType.DateTime, model.StartTime);
            db.AddInParameter(dbCommand, "EndTime", DbType.DateTime, model.EndTime);
            db.AddInParameter(dbCommand, "Days", DbType.Decimal, model.Days);
            db.AddInParameter(dbCommand, "VacationType", DbType.AnsiString, model.VacationType);
            db.AddInParameter(dbCommand, "Reason", DbType.String, model.Reason);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Hr_VacationRecord ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Hr_VacationRecord GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,UserId,UserName,DepartId,DeaprtName,DateTime,StartTime,EndTime,Days,VacationType,Reason from Hr_VacationRecord ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Hr.Entity.Hr_VacationRecord model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,UserId,UserName,DepartId,DeaprtName,DateTime,StartTime,EndTime,Days,VacationType,Reason ");
            strSql.Append(" FROM Hr_VacationRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "Hr_VacationRecord");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }*/

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Hr.Entity.Hr_VacationRecord> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,UserId,UserName,DepartId,DeaprtName,DateTime,StartTime,EndTime,Days,VacationType,Reason ");
            strSql.Append(" FROM Hr_VacationRecord ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Hr.Entity.Hr_VacationRecord> list = new List<Chenxin.Lib.Hr.Entity.Hr_VacationRecord>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Hr_VacationRecord ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Hr.Entity.Hr_VacationRecord model = new Chenxin.Lib.Hr.Entity.Hr_VacationRecord();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["UserId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UserId = new Guid(ojb.ToString());
            }
            model.UserName = dataReader["UserName"].ToString();
            ojb = dataReader["DepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartId = new Guid(ojb.ToString());
            }
            model.DepartName = dataReader["DepartName"].ToString();
            ojb = dataReader["DateTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DateTime = (DateTime)ojb;
            }
            ojb = dataReader["StartTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.StartTime = (DateTime)ojb;
            }
            ojb = dataReader["EndTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.EndTime = (DateTime)ojb;
            }
            ojb = dataReader["Days"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Days = (decimal)ojb;
            }
            model.VacationType = dataReader["VacationType"].ToString();
            model.Reason = dataReader["Reason"].ToString();
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strTableName,string strSelect, string TableFieldList, string strWhere, string strOrder, int intCurPage, int intRowCnt, Boolean bDoCount)
        {

            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, intCurPage);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, intRowCnt);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, strOrder);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, strTableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, strWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, strSelect);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, bDoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        #endregion  成员方法
    }
}

